package com.panghl.util;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


/**
 * 图片压缩
 */
public class ResizeImage {
    public static void main(String[] args) throws IOException {
        //windows路径,linux环境下相应修改
//        String outputFolder = "D:/mcms/upload/"; //输出目录
        String fileName = "C:\\Users\\phl04\\Desktop\\测试E-51302119980425001X-20210624-17_17-enclosure\\身份证正面00.jpg";
//        ResizeImage r = new ResizeImage();
//        int toWidth = 770, toHeight = 593;
        BufferedImage imageList = ResizeImage.getImageList(fileName, new String[]{"jpg", "png", "gif"});
        ResizeImage.writeHighQuality("newFile.jpg",
                ResizeImage.zoomImage(imageList, 770, 593),
                "D:/");


//        log.info("缩略图处理");
//        //缩略图处理
//        String smallFileName = filePath.substring(filePath.lastIndexOf("/") + 1, filePath.lastIndexOf("."));
//
//        String suffix = filePath.substring(filePath.lastIndexOf("."));
//        String samllName = smallFileName + "-770-593" + suffix;
//        BufferedImage imageList = ResizeImage.getImageList(filePath, new String[]{"jpg", "png", "gif"});
//        if (System.getProperty("os.name").toLowerCase().startsWith("win")) {
//            ResizeImage.writeHighQuality(samllName, ResizeImage.zoomImage(imageList, 770, 593), fileRootPathWin);
//        } else {
//            log.info("缩略图处理--lin");
//            ResizeImage.writeHighQuality(samllName, ResizeImage.zoomImage(imageList, 770, 593), fileRootPathLin);
//            log.info("缩略图处理--lin--成功");
//        }

    }

    /**
     * 要转化的图像的文件夹,就是存放图像的文件夹路径
     * @Description: 取得图片对象
     * @date 2017年5月7日10:48:27
     */
    public static BufferedImage zoomImage(BufferedImage im, int toWidth, int toHeight) {
        BufferedImage result = new BufferedImage(toWidth, toHeight, BufferedImage.TYPE_INT_RGB);
        result.getGraphics().drawImage(im.getScaledInstance(toWidth, toHeight, java.awt.Image.SCALE_SMOOTH), 0, 0, null);
        return result;
    }

    /**
     *  要转化的图像的文件夹,就是存放图像的文件夹路径
     * @Description: 取得图片对象
     * @date 2017年5月7日10:48:27
     */
    public static BufferedImage getImageList(String ImgList, String[] type) throws IOException {
        Map<String, Boolean> map = new HashMap<String, Boolean>();
        for (String s : type) {
            map.put(s, true);
        }
        BufferedImage imageList = null;
        File file = null;
        file = new File(ImgList);
        try {
            if (file.length() != 0 && map.get(getExtension(file.getName())) != null) {
                imageList = ImageIO.read(file);
            }
        } catch (Exception e) {
            imageList = null;
        }
        return imageList;
    }

    /**
     * 把图片写到磁盘上
     *
     * @param im
     * @param path     图片写入的文件夹地址
     * @param fileName 写入图片的名字
     * @date 2017年5月7日10:48:27
     */
    public static boolean writeToDisk(BufferedImage im, String path, String fileName) {
        File f = new File(path + fileName);
        String fileType = getExtension(fileName);
        if (fileType == null) {
            return false;
        }
        try {
            ImageIO.write(im, fileType, f);
            im.flush();
            return true;
        } catch (IOException e) {
            return false;
        }
    }

    /**
     *  String path , BufferedImage im, String fileFullPath
     * @Description: 生成图片
     * @date 2017年5月7日10:48:27
     */
    public static boolean writeHighQuality(String path, BufferedImage im, String fileFullPath) throws IOException {
        FileOutputStream newimage = null;
        try {
            // 输出到文件流
            newimage = new FileOutputStream(fileFullPath + path);
//            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
//            JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(im);
//            // 压缩质量
//            jep.setQuality(1f, true);
//            encoder.encode(im, jep);
            ImageIO.write(im,  "jpg", newimage);
            //近JPEG编码
            newimage.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * @param  fileName 格式如：cn1100000213EA_1_xnl.jpg
     * @Description: 取文件名的后缀
     * @date 2017年5月7日10:48:27
     */
    public static String getExtension(String fileName) {
        try {
            return fileName.split("\\.")[fileName.split("\\.").length - 1];
        } catch (Exception e) {
            return null;
        }
    }
}
